package artid.com.arttogoa;

import artid.com.arttogoa.R;

import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.util.ToastUtil;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：NewMessageActivity   
 * 类描述：新消息通知   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午2:28:24      
 *
 */
public class NewMessageActivity extends BaseActivity implements OnClickListener{
	private CheckBox notification_cb,nodisturbing_cb,sound_cb,vibration_cb;
	private Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_message);
		mContext=NewMessageActivity.this;
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("新消息通知");
		setBack();
		initView();
	}

	private void initView(){
		notification_cb=(CheckBox) findViewById(R.id.newmessage_notification);
		nodisturbing_cb=(CheckBox) findViewById(R.id.newmessage_nodisturbing);
		sound_cb=(CheckBox) findViewById(R.id.newmessage_sound);
		vibration_cb=(CheckBox) findViewById(R.id.newmessage_vibration);
		if(setPushSetting(1)){
		notification_cb.setChecked(setPushSetting(1));
		nodisturbing_cb.setChecked(setPushSetting(2));
		sound_cb.setChecked(setPushSetting(3));
		vibration_cb.setChecked(setPushSetting(4));
		}else{
			notification_cb.setChecked(false);
			nodisturbing_cb.setChecked(false);
			sound_cb.setChecked(false);
			vibration_cb.setChecked(false);
		}
		//这个监听事件位置可以改变必须放置在默认设置之后不然会死循环
		notification_cb.setOnClickListener(this);
		nodisturbing_cb.setOnClickListener(this);
		sound_cb.setOnClickListener(this);
		vibration_cb.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newmessage_notification:
			//新消息通知
			if(setPushSetting(1)){
				MySharedPreferences.getMySharedPreferences(mContext).setValue("NewMessageNotification"+ AppUtil.getUser().getUid(), "false");
				notification_cb.setChecked(false);
				nodisturbing_cb.setChecked(false);
				sound_cb.setChecked(false);
				vibration_cb.setChecked(false);
			}else{
				MySharedPreferences.getMySharedPreferences(mContext).setValue("NewMessageNotification"+AppUtil.getUser().getUid(), "true");
				notification_cb.setChecked(true);
				if(setPushSetting(2)){
					MySharedPreferences.getMySharedPreferences(mContext).getValue("NoDisturbing"+AppUtil.getUser().getUid(), setPushSetting(2)+"");
					nodisturbing_cb.setChecked(setPushSetting(2));
				}
				if(setPushSetting(3)){
					MySharedPreferences.getMySharedPreferences(mContext).getValue("Sound"+AppUtil.getUser().getUid(), setPushSetting(3)+"");
					sound_cb.setChecked(setPushSetting(3));
				}
				if(setPushSetting(4)){
					MySharedPreferences.getMySharedPreferences(mContext).getValue("Vibration"+AppUtil.getUser().getUid(), setPushSetting(4)+"");
					vibration_cb.setChecked(setPushSetting(4));
				}
			}
			ArtWorkApp.setEaseUIIsOpenNotification();
			break;
		case R.id.newmessage_nodisturbing:
			//夜间免打扰
			if(setPushSetting(1)){
				if(setPushSetting(2)){
					nodisturbing_cb.setChecked(false);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("NoDisturbing"+AppUtil.getUser().getUid(), "false");
				}else{
					nodisturbing_cb.setChecked(true);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("NoDisturbing"+AppUtil.getUser().getUid(), "true");
				}
				ArtWorkApp.setEaseUIIsOpenNotification();  
			}else{
				ToastUtil.toast(mContext, "请先开启新消息通知之后才可以进行设置");
			}

			break;
		case R.id.newmessage_sound:
			//声音设置
			if(setPushSetting(1)){
				if(setPushSetting(3)){
					sound_cb.setChecked(false);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("Sound"+AppUtil.getUser().getUid(), "false");
				}else{
					sound_cb.setChecked(true);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("Sound"+AppUtil.getUser().getUid(), "true");
				}
				ArtWorkApp.setEaseUIIsOpenNotification();   
			}else{
				ToastUtil.toast(mContext, "请先开启新消息通知之后才可以进行设置");
			}
			break;
		case R.id.newmessage_vibration:
			//震动设置
			if(setPushSetting(1)){
				if(setPushSetting(4)){
					vibration_cb.setChecked(false);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("Vibration"+AppUtil.getUser().getUid(), "false");
				}else{
					vibration_cb.setChecked(true);
					MySharedPreferences.getMySharedPreferences(mContext).setValue("Vibration"+AppUtil.getUser().getUid(), "true");
				}
				ArtWorkApp.setEaseUIIsOpenNotification();
			}else{
				ToastUtil.toast(mContext, "请先开启新消息通知之后才可以进行设置");
			}
			break;
		default:
			break;
		}

	}

	/**
	 * 设置推送是否响铃、震动、通知栏是否显示
	 */
	public Boolean setPushSetting(int type){
		int NewMessageNotification=1;//通知栏
		int NoDisturbing =2;//免打扰
		int Sound=3;//铃声
		int Vibration=4;//震动
		if(NewMessageNotification==type){
			if("true".equals(MySharedPreferences.getMySharedPreferences(mContext).getValue("NewMessageNotification"+AppUtil.getUser().getUid(), "true"))){
				return true;
			}else{
				return false;
			}
		}else if(NoDisturbing==type){
			if("true".equals(MySharedPreferences.getMySharedPreferences(mContext).getValue("NoDisturbing"+AppUtil.getUser().getUid(), "false"))){
				return true;
			}else{
				return false;
			}
		}else if(Sound==type){
			if("true".equals(MySharedPreferences.getMySharedPreferences(mContext).getValue("Sound"+AppUtil.getUser().getUid(), "true"))){
				return true;
			}else{
				return false;
			}
		}else if(Vibration==type){
			if("true".equals(MySharedPreferences.getMySharedPreferences(mContext).getValue("Vibration"+AppUtil.getUser().getUid(), "true"))){
				return true;
			}else{
				return false;
			}
		}
		return true;
	}

}
